package com.drools.freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by carlkong on 2016/7/19.
 */
public class FreeMarkerTool {
    private Configuration cfg;            //模版配置对象

    public void init() throws Exception {
        //初始化FreeMarker配置
        //创建一个Configuration实例
        cfg = new Configuration();
        //设置FreeMarker的模版文件夹位置 D:\workspace\DemoDrools\src\main\resource\template\rule.ftl
        cfg.setDirectoryForTemplateLoading(new File("src\\main\\resource\\template"));
    }

    public void process(Writer output) throws Exception {
        //构造填充数据的Map
        Map map = new HashMap();
        map.put("rule_name1", "boy");
        map.put("age1", "0");
        map.put("desc1", "少年");

        map.put("rule_name2", "youth");
        map.put("age2", "12");
        map.put("desc2", "青年");

        map.put("rule_name3", "midlife");
        map.put("age3", "24");
        map.put("desc3", "中年");

        map.put("rule_name4", "old");
        map.put("age4", "60");
        map.put("desc4", "老年");
        //创建模版对象
        Template t = cfg.getTemplate("rule.ftl");
        //在模版上执行插值操作，并输出到制定的输出流中
        t.process(map, output);
    }
}
